package com.hckj.medical.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hckj.medical.entity.Prescription;
import com.hckj.medical.entity.Scheme;
import com.hckj.medical.mapper.PrescriptionMapper;
import com.hckj.medical.mapper.SchemeMapper;
import com.hckj.medical.service.IPrescriptionService;
import com.hckj.medical.utils.GetDate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author hckj
 * @since 2023-11-25
 */
@Service
public class PrescriptionServiceImpl extends ServiceImpl<PrescriptionMapper, Prescription> implements IPrescriptionService {
    @Autowired
    private PrescriptionMapper prescriptionMapper;
    @Autowired
    private SchemeMapper schemeMapper;
    @Override
    public List<Map> findBypreId(Integer id) {
        return prescriptionMapper.selectBypid(id);
    }


    @Override
    public List<Integer> findAllmoth(Integer did) {
        Calendar calendar = Calendar.getInstance(); // 创建一个Calendar对象
        int year = calendar.get(Calendar.YEAR);
        List<Integer> integers = new ArrayList<>();
        for (int i = 1; i <= 12; i++) {
            String date = year + "年" + i + "月";
            int i1 = prescriptionMapper.selectByDoctorAndId(did, date);
            integers.add(i1);
        }
        return integers;
    }

    @Transactional
    @Override
    public void insertPreandUpdateSchem(Prescription prescription) {
        String time = GetDate.getTime();
        prescription.setTreatdate(time);
        prescriptionMapper.insert(prescription);
        Scheme scheme = new Scheme();
        scheme.setId(prescription.getSchemeId());
        scheme.setNumber(prescription.getFrequency());
        schemeMapper.updateById(scheme);
    }
}
